﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DataLib.DBUtility;

namespace DataLib.DAL
{
	/// <summary>
	/// 数据访问类:StoragePostion
	/// </summary>
	public partial class StoragePostion
	{
		public StoragePostion()
		{}
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "StoragePostion");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int? id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from StoragePostion");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DataLib.Model.StoragePostion model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into StoragePostion(");
            strSql.Append("weightUnit,storagecode,goodstype,storagetype,maxvalume,alertvalume,maxweight,alertweight,valumeunit,currweight,currvalume,belongCity,graphicCity)");
            strSql.Append(" values (");
            strSql.Append("@weightUnit,@storagecode,@goodstype,@storagetype,@maxvalume,@alertvalume,@maxweight,@alertweight,@valumeunit,@currweight,@currvalume,@belongCity,@graphicCity)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@weightUnit", SqlDbType.NVarChar,30),
					new SqlParameter("@storagecode", SqlDbType.NVarChar,30),
					new SqlParameter("@goodstype", SqlDbType.NVarChar,30),
					new SqlParameter("@storagetype", SqlDbType.NVarChar,30),
					new SqlParameter("@maxvalume", SqlDbType.Int,4),
					new SqlParameter("@alertvalume", SqlDbType.Int,4),
					new SqlParameter("@maxweight", SqlDbType.Decimal,9),
					new SqlParameter("@alertweight", SqlDbType.Decimal,9),
					new SqlParameter("@valumeunit", SqlDbType.NVarChar,30),
					new SqlParameter("@currweight", SqlDbType.Decimal,9),
					new SqlParameter("@currvalume", SqlDbType.Int,4),
					new SqlParameter("@belongCity", SqlDbType.NVarChar,30),
					new SqlParameter("@graphicCity", SqlDbType.NVarChar,30)};

            if (model.alertvalume == null)
            {
                model.alertvalume = 0;
            }
            if (model.alertweight == null)
            {
                model.alertweight = 0;
            }
            parameters[0].Value = model.weightUnit;
            parameters[1].Value = model.storagecode;
            parameters[2].Value = model.goodstype;
            parameters[3].Value = model.storagetype;
            parameters[4].Value = model.maxvalume;
            parameters[5].Value = model.alertvalume;
            parameters[6].Value = model.maxweight;
            parameters[7].Value = model.alertweight;
            parameters[8].Value = model.valumeunit;
            parameters[9].Value = model.currweight;
            parameters[10].Value = model.currvalume;
            parameters[11].Value = model.belongCity;
            parameters[12].Value = model.graphicCity;


            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DataLib.Model.StoragePostion model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update StoragePostion set ");
            strSql.Append("weightUnit=@weightUnit,");
            strSql.Append("storagecode=@storagecode,");
            strSql.Append("goodstype=@goodstype,");
            strSql.Append("storagetype=@storagetype,");
            strSql.Append("maxvalume=@maxvalume,");
            strSql.Append("alertvalume=@alertvalume,");
            strSql.Append("maxweight=@maxweight,");
            strSql.Append("alertweight=@alertweight,");
            strSql.Append("valumeunit=@valumeunit,");
            strSql.Append("currweight=@currweight,");
            strSql.Append("currvalume=@currvalume,");
            strSql.Append("belongCity=@belongCity,");
            strSql.Append("graphicCity=@graphicCity");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@weightUnit", SqlDbType.NVarChar,30),
					new SqlParameter("@storagecode", SqlDbType.NVarChar,30),
					new SqlParameter("@goodstype", SqlDbType.NVarChar,30),
					new SqlParameter("@storagetype", SqlDbType.NVarChar,30),
					new SqlParameter("@maxvalume", SqlDbType.Int,4),
					new SqlParameter("@alertvalume", SqlDbType.Int,4),
					new SqlParameter("@maxweight", SqlDbType.Decimal,9),
					new SqlParameter("@alertweight", SqlDbType.Decimal,9),
					new SqlParameter("@valumeunit", SqlDbType.NVarChar,30),
					new SqlParameter("@currweight", SqlDbType.Decimal,9),
					new SqlParameter("@currvalume", SqlDbType.Int,4),
					new SqlParameter("@belongCity", SqlDbType.NVarChar,30),
					new SqlParameter("@graphicCity", SqlDbType.NVarChar,30),
					new SqlParameter("@id", SqlDbType.Int,4)};
            if (model.alertvalume == null)
            {
                model.alertvalume = 0;
            }
            if (model.alertweight == null)
            {
                model.alertweight = 0;
            } parameters[0].Value = model.weightUnit;
            parameters[1].Value = model.storagecode;
            parameters[2].Value = model.goodstype;
            parameters[3].Value = model.storagetype;
            parameters[4].Value = model.maxvalume;
            parameters[5].Value = model.alertvalume;
            parameters[6].Value = model.maxweight;
            parameters[7].Value = model.alertweight;
            parameters[8].Value = model.valumeunit;
            parameters[9].Value = model.currweight;
            parameters[10].Value = model.currvalume;
            parameters[11].Value = model.belongCity;
            parameters[12].Value = model.graphicCity;
            parameters[13].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int? id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StoragePostion ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StoragePostion ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.StoragePostion GetModel(int? id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,weightUnit,storagecode,goodstype,storagetype,maxvalume,alertvalume,maxweight,alertweight,valumeunit,currweight,currvalume,belongCity,graphicCity from StoragePostion ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            DataLib.Model.StoragePostion model = new DataLib.Model.StoragePostion();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.StoragePostion DataRowToModel(DataRow row)
        {
            DataLib.Model.StoragePostion model = new DataLib.Model.StoragePostion();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["weightUnit"] != null)
                {
                    model.weightUnit = row["weightUnit"].ToString();
                }
                if (row["storagecode"] != null)
                {
                    model.storagecode = row["storagecode"].ToString();
                }
                if (row["goodstype"] != null)
                {
                    model.goodstype = row["goodstype"].ToString();
                }
                if (row["storagetype"] != null)
                {
                    model.storagetype = row["storagetype"].ToString();
                }
                if (row["maxvalume"] != null && row["maxvalume"].ToString() != "")
                {
                    model.maxvalume = int.Parse(row["maxvalume"].ToString());
                }
                if (row["alertvalume"] != null && row["alertvalume"].ToString() != "")
                {
                    model.alertvalume = int.Parse(row["alertvalume"].ToString());
                }
                if (row["maxweight"] != null && row["maxweight"].ToString() != "")
                {
                    model.maxweight = decimal.Parse(row["maxweight"].ToString());
                }
                if (row["alertweight"] != null && row["alertweight"].ToString() != "")
                {
                    model.alertweight = decimal.Parse(row["alertweight"].ToString());
                }
                if (row["valumeunit"] != null)
                {
                    model.valumeunit = row["valumeunit"].ToString();
                }
                if (row["currweight"] != null && row["currweight"].ToString() != "")
                {
                    model.currweight = decimal.Parse(row["currweight"].ToString());
                }
                if (row["currvalume"] != null && row["currvalume"].ToString() != "")
                {
                    model.currvalume = int.Parse(row["currvalume"].ToString());
                }
                if (row["belongCity"] != null)
                {
                    model.belongCity = row["belongCity"].ToString();
                }
                if (row["graphicCity"] != null)
                {
                    model.graphicCity = row["graphicCity"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,weightUnit,storagecode,goodstype,storagetype,maxvalume,alertvalume,maxweight,alertweight,valumeunit,currweight,currvalume,belongCity,graphicCity ");
            strSql.Append(" FROM StoragePostion ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,weightUnit,storagecode,goodstype,storagetype,maxvalume,alertvalume,maxweight,alertweight,valumeunit,currweight,currvalume,belongCity,graphicCity ");
            strSql.Append(" FROM StoragePostion ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM StoragePostion ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from StoragePostion T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "StoragePostion";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
		#region  ExtensionMethod
        public Model.StoragePostion GetModelByCode(string storCode)
        {

            DataTable dt = GetList(1, "storagecode='" + storCode + "'", "id desc").Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {

                return DataRowToModel(dt.Rows[0]);
            }
            else

            {
                return null;
            }
        }
		#endregion  ExtensionMethod
	}
}

